import {
    openIndexedDB,
    addDb,
    getDataByKey,
    updateDB,
    deleteDB,
    getData,
    cursorDelete,
} from "@/utils/IndexedDB";
let ws = null
let maxReConnectTimes = null;
let lockReconnect = false;
let wsUrl = null;
let needReconnect = null;
let messageCallBack = null;

const initWs = () => {
    const token = localStorage.getItem('token')
    if (!token) {
        needReconnect = true;
        return
    }
    let proto = "ws"
    if (location.protocol === "https:") {
        proto = "wss"
    }
    wsUrl = `${proto}://${location.host}/ws?token=${token}`;
    needReconnect = true;
    maxReConnectTimes = 5;
    createWs();
}

const createWs = () => {
    if (wsUrl == null) {
        return
    };
    ws = new WebSocket(wsUrl);
    ws.onopen = function () {
        console.log('客户端连接ws成功')
        maxReConnectTimes = 5
    };
    ws.onmessage = async function (e) {
        const message = JSON.parse(e.data)
        const commandCode = message.commandCode
        let chatDb = await openIndexedDB("chat" + JSON.parse(localStorage.getItem("userInfo")).userId);
        switch (commandCode) {
            case 10000:
                const chatUserList = message.data.chatUserList;
                for (let i = 0; i < chatUserList.length; i++) {
                    const chatUserInfo = chatUserList[i];
                    const existChatUser = await getDataByKey(chatDb, "chatUser", chatUserInfo.contactId);
                    if (existChatUser) {
                        existChatUser.contactIcon = chatUserInfo.contactIcon;
                        existChatUser.contactName = chatUserInfo.contactName;
                        existChatUser.lastMessage = chatUserInfo.lastMessage;
                        existChatUser.lastReceiveTime = chatUserInfo.lastReceiveTime;
                        existChatUser.lastSendName = chatUserInfo.lastSendName;
                        existChatUser.memberCount = chatUserInfo.memberCount;
                        if (existChatUser.clean == 1) {
                            updateDB(chatDb, "chatUser", existChatUser)
                        }
                    } else {
                        let addData = {
                            chatId: chatUserInfo.chatId,
                            contactIcon: chatUserInfo.contactIcon,
                            contactId: chatUserInfo.contactId,
                            contactName: chatUserInfo.contactName,
                            contactType: chatUserInfo.contactType,
                            lastMessage: chatUserInfo.lastMessage,
                            lastReceiveTime: chatUserInfo.lastReceiveTime,
                            lastSendName: chatUserInfo.lastSendName,
                            memberCount: chatUserInfo.memberCount,
                            userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                            status: 1,
                            clean: 1,
                            noReadCount: 0,
                            topType: 0,
                            disable: 0,
                        };
                        addDb(chatDb, "chatUser", addData);
                    }
                }
                const messageList = message.data.chatMessageList;
                for (let i = 0; i < messageList.length; i++) {
                    const messageListItem = messageList[i];
                    messageListItem.msgState = 1
                    addDb(chatDb, "message", messageListItem);
                }
                const userId = JSON.parse(localStorage.getItem("userInfo")).userId
                const existUser = await getDataByKey(chatDb, "user", userId);
                if (!existUser) {
                    let addUser = {
                        userId: userId,
                        contactNoRead: 0
                    };
                    await addDb(chatDb, "user", addUser)
                }
                const userApplyCount = message.data.applyCount;
                if (userApplyCount) {
                    let updateUser = {
                        userId: userId,
                        contactNoRead: userApplyCount + existUser.contactNoRead
                    };
                    await updateDB(chatDb, "user", updateUser)
                }
                if (message.data.contactReadList.length !== 0) {
                    message.data.contactReadList.forEach(async (contactId) => {
                        const setChatUserItem = await getDataByKey(chatDb, "chatUser", contactId);
                        const messageListData = await getData(
                            chatDb,
                            "message",
                            "chatId",
                            setChatUserItem.chatId
                        );
                        messageListData
                            .filter((item) => item.msgState == 1)
                            .forEach((item) => {
                                item.msgState = 2;
                                updateDB(chatDb, "message", item);
                            });
                    });
                }
                const chatUserCountMap = {};
                messageList.forEach(item => {
                    let contactId = item.contactType == 1 ? item.contactId : item.sendUserId;
                    let noReadCount = chatUserCountMap[contactId];
                    if (!noReadCount) {
                        chatUserCountMap[contactId] = 1;
                    } else {
                        chatUserCountMap[contactId] = noReadCount + 1;
                    }
                });
                for (let item in chatUserCountMap) {
                    const existChatUser = await getDataByKey(chatDb, "chatUser", item);
                    existChatUser.noReadCount = existChatUser.noReadCount + chatUserCountMap[item];
                    updateDB(chatDb, "chatUser", existChatUser)
                }
                break;
            case 10005:
                message.data.msgState = 1
                addDb(chatDb, "message", message.data);
                const existChatUserSelf = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserSelf) {
                    existChatUserSelf.contactId = message.data.contactId
                    existChatUserSelf.contactType = message.data.contactType
                    existChatUserSelf.chatId = message.data.chatId
                    existChatUserSelf.contactName = message.data.contactUsername
                    existChatUserSelf.contactIcon = message.data.contactUserIcon
                    existChatUserSelf.lastMessage = message.data.messageContent
                    existChatUserSelf.lastReceiveTime = message.data.sendTime
                    existChatUserSelf.clean = 1
                    if (message.data.messageType === 'image') {
                        existChatUserSelf.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        existChatUserSelf.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        existChatUserSelf.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        existChatUserSelf.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        existChatUserSelf.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        existChatUserSelf.lastMessage = message.data.messageContent
                    }
                    updateDB(chatDb, "chatUser", existChatUserSelf)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.contactUserIcon,
                        contactId: message.data.contactId,
                        contactName: message.data.contactUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.lastMessage,
                        lastReceiveTime: message.data.lastReceiveTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: 0,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    if (message.data.messageType === 'image') {
                        addData.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        addData.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        addData.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        addData.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        addData.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        addData.lastMessage = message.data.messageContent
                    }
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10006:
                let chatDbPrivatContact = await openIndexedDB("chat" + message.data.contactId);
                message.data.msgState = 1
                addDb(chatDbPrivatContact, "message", message.data);
                const existChatUserContact = await getDataByKey(chatDbPrivatContact, "chatUser", message.data.sendUserId);
                if (existChatUserContact) {
                    existChatUserContact.contactId = message.data.sendUserId
                    existChatUserContact.contactType = message.data.contactType
                    existChatUserContact.chatId = message.data.chatId
                    existChatUserContact.contactName = message.data.sendUsername
                    existChatUserContact.contactIcon = message.data.sendUserIcon
                    existChatUserContact.lastMessage = message.data.messageContent
                    existChatUserContact.lastReceiveTime = message.data.sendTime
                    existChatUserContact.status = 1
                    existChatUserContact.clean = 1
                    if (message.data.messageType === 'image') {
                        existChatUserContact.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        existChatUserContact.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        existChatUserContact.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        existChatUserContact.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        existChatUserContact.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        existChatUserContact.lastMessage = message.data.messageContent
                    }
                    updateDB(chatDbPrivatContact, "chatUser", existChatUserContact)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.sendUserIcon,
                        contactId: message.data.sendUserId,
                        contactName: message.data.sendUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.messageContent,
                        lastReceiveTime: message.data.sendTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: 0,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    if (message.data.messageType === 'image') {
                        addData.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        addData.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        addData.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        addData.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        addData.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        addData.lastMessage = message.data.messageContent
                    }
                    addDb(chatDbPrivatContact, "chatUser", addData);
                }
                break;
            case 10007:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const existChatUserGroupSelf = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserGroupSelf) {
                    existChatUserGroupSelf.contactId = message.data.contactId
                    existChatUserGroupSelf.contactType = message.data.contactType
                    existChatUserGroupSelf.chatId = message.data.chatId
                    existChatUserGroupSelf.contactName = message.data.contactUsername
                    existChatUserGroupSelf.contactIcon = message.data.contactUserIcon
                    existChatUserGroupSelf.lastMessage = message.data.messageContent
                    existChatUserGroupSelf.lastReceiveTime = message.data.sendTime
                    existChatUserGroupSelf.status = 1
                    if (message.data.messageType === 'image') {
                        existChatUserGroupSelf.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        existChatUserGroupSelf.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        existChatUserGroupSelf.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        existChatUserGroupSelf.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        existChatUserGroupSelf.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        existChatUserGroupSelf.lastMessage = message.data.messageContent
                    }
                    updateDB(chatDb, "chatUser", existChatUserGroupSelf)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.contactUserIcon,
                        contactId: message.data.contactId,
                        contactName: message.data.contactUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.lastMessage,
                        lastReceiveTime: message.data.lastReceiveTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: 0,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    if (message.data.messageType === 'image') {
                        addData.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        addData.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        addData.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        addData.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        addData.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        addData.lastMessage = message.data.messageContent
                    }
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10008:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const existChatUserGroupContact = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserGroupContact) {
                    existChatUserGroupContact.contactId = message.data.contactId
                    existChatUserGroupContact.contactType = message.data.contactType
                    existChatUserGroupContact.chatId = message.data.chatId
                    existChatUserGroupContact.contactName = message.data.contactUsername
                    existChatUserGroupContact.contactIcon = message.data.contactUserIcon
                    existChatUserGroupContact.lastMessage = message.data.messageContent
                    existChatUserGroupContact.lastReceiveTime = message.data.sendTime
                    existChatUserGroupContact.status = 1
                    if (message.data.messageType === 'withdraw' || message.data.messageType === 'common') {
                        existChatUserGroupContact.lastSendName = "";
                    } else {
                        existChatUserGroupContact.lastSendName = message.data.sendUsername;
                    }
                    if (message.data.messageType === 'image') {
                        existChatUserGroupContact.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        existChatUserGroupContact.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        existChatUserGroupContact.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        existChatUserGroupContact.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        existChatUserGroupContact.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        existChatUserGroupContact.lastMessage = message.data.messageContent
                    }
                    updateDB(chatDb, "chatUser", existChatUserGroupContact)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.contactUserIcon,
                        contactId: message.data.contactId,
                        contactName: message.data.contactUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.messageContent,
                        lastReceiveTime: message.data.sendTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: 0,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    if (message.data.messageType === 'image') {
                        addData.lastMessage = '[图片]'
                    } else if (message.data.messageType === 'video') {
                        addData.lastMessage = '[视频]'
                    } else if (message.data.messageType === 'file') {
                        addData.lastMessage = '[文件]'
                    } else if (message.data.messageType === 'voice') {
                        addData.lastMessage = '[语音]'
                    } else if (message.data.messageType === "withdraw") {
                        addData.lastMessage =
                            "`" + message.data.sendUsername + "`" + "撤回了一条消息"
                    } else if (message.data.messageType === 'reply') {
                        addData.lastMessage = message.data.messageContent
                    }
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10016:
                const messageListData = await getData(chatDb, "message", "chatId", message.data);
                messageListData.filter((item) => item.msgState == 1).forEach((item) => {
                    item.msgState = 2;
                    updateDB(chatDb, "message", item);
                })
                break;
            case 10001:
                const currentUserId = JSON.parse(localStorage.getItem("userInfo")).userId
                const existCurrentUser = await getDataByKey(chatDb, "user", currentUserId);
                if (existCurrentUser) {
                    let updateCurrentUser = {
                        userId: currentUserId,
                        contactNoRead: existCurrentUser.contactNoRead + 1
                    };
                    await updateDB(chatDb, "user", updateCurrentUser)
                }
                break;
            case 10002:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const existChatUserAddSelf = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserAddSelf) {
                    existChatUserAddSelf.contactId = message.data.contactId
                    existChatUserAddSelf.contactType = message.data.contactType
                    existChatUserAddSelf.chatId = message.data.chatId
                    existChatUserAddSelf.contactName = message.data.contactUsername
                    existChatUserAddSelf.contactIcon = message.data.contactUserIcon
                    existChatUserAddSelf.lastMessage = message.data.messageContent
                    existChatUserAddSelf.lastReceiveTime = message.data.sendTime
                    if (message.data.contactType === 0) {
                        existChatUserAddSelf.memberCount = 0;
                    } else if (message.data.contactType === 1) {
                        existChatUserAddSelf.memberCount = message.data.memberCount;
                    }
                    updateDB(chatDb, "chatUser", existChatUserAddSelf)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.contactUserIcon,
                        contactId: message.data.contactId,
                        contactName: message.data.contactUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.lastMessage,
                        lastReceiveTime: message.data.lastReceiveTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: message.data.memberCount,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10003:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const existChatUserAddReciver = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserAddReciver) {
                    existChatUserAddReciver.contactId = message.data
                    existChatUserAddReciver.contactType = message.data.contactType
                    existChatUserAddReciver.chatId = message.data.chatId
                    existChatUserAddReciver.contactName = message.data.sendUsername
                    existChatUserAddReciver.contactIcon = message.data.sendUserIcon
                    existChatUserAddReciver.lastMessage = message.data.messageContent
                    existChatUserAddReciver.lastReceiveTime = message.data.sendTime
                    if (message.data.contactType === 0) {
                        existChatUserAddReciver.memberCount = 0;
                    } else if (message.data.contactType === 1) {
                        existChatUserAddReciver.memberCount = message.data.memberCount;
                    }
                    updateDB(chatDb, "chatUser", existChatUserAddReciver)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.sendUserIcon,
                        contactId: message.data.sendUserId,
                        contactName: message.data.sendUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.lastMessage,
                        lastReceiveTime: message.data.lastReceiveTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: message.data.memberCount,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10009:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const chatUserCreateGroup = {};
                chatUserCreateGroup.contactId = message.data.contactId;
                chatUserCreateGroup.contactType = message.data.contactType;
                chatUserCreateGroup.chatId = message.data.chatId;
                chatUserCreateGroup.contactName = message.data.contactUsername;
                chatUserCreateGroup.contactIcon = message.data.contactUserIcon;
                chatUserCreateGroup.lastMessage = message.data.messageContent;
                chatUserCreateGroup.lastReceiveTime = message.data.sendTime;
                chatUserCreateGroup.noReadCount = 1;
                chatUserCreateGroup.memberCount = 1;
                chatUserCreateGroup.status = 1;
                chatUserCreateGroup.clean = 1;
                chatUserCreateGroup.disable = 0;
                chatUserCreateGroup.topType = 0;
                chatUserCreateGroup.userId = JSON.parse(localStorage.getItem("userInfo")).userId;
                addDb(chatDb, "chatUser", chatUserCreateGroup);
                break;
            case 10010:
            case 10011:
            case 10012:
            case 10013:
                message.data.msgState = 1;
                addDb(chatDb, "message", message.data);
                const existChatUserAddGroup = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                if (existChatUserAddGroup) {
                    existChatUserAddGroup.contactId = message.data.contactId
                    existChatUserAddGroup.contactType = message.data.contactType
                    existChatUserAddGroup.chatId = message.data.chatId
                    existChatUserAddGroup.contactName = message.data.contactUsername
                    existChatUserAddGroup.contactIcon = message.data.contactUserIcon
                    existChatUserAddGroup.lastMessage = message.data.messageContent
                    existChatUserAddGroup.lastReceiveTime = message.data.sendTime
                    existChatUserAddGroup.lastSendName = ""
                    if (message.data.contactType === 0) {
                        existChatUserAddGroup.memberCount = 0;
                    } else if (message.data.contactType === 1) {
                        existChatUserAddGroup.memberCount = message.data.memberCount;
                    }
                    if (message.commandCode == 10011) {
                        existChatUserAddGroup.disable = 1
                    }
                    updateDB(chatDb, "chatUser", existChatUserAddGroup)
                } else {
                    let addData = {
                        chatId: message.data.chatId,
                        contactIcon: message.data.contactUserIcon,
                        contactId: message.data.contactId,
                        contactName: message.data.contactUsername,
                        contactType: message.data.contactType,
                        lastMessage: message.data.lastMessage,
                        lastReceiveTime: message.data.lastReceiveTime,
                        lastSendName: message.data.lastSendName,
                        memberCount: message.data.memberCount,
                        userId: JSON.parse(localStorage.getItem("userInfo")).userId,
                        status: 1,
                        clean: 1,
                        noReadCount: 1,
                        topType: 0,
                        disable: 0,
                    };
                    if (message.commandCode == 10011) {
                        addData.disable = 1
                        addData.lastSendName = ""
                    }
                    addDb(chatDb, "chatUser", addData);
                }
                break;
            case 10017:
            case 10018:
                const messageListDataTop = await getData(chatDb, "message", "chatId", message.data.chatId);
                messageListDataTop.filter((item) => item.msgTop == 2).forEach((item) => {
                    item.msgTop = 1;
                    updateDB(chatDb, "message", item);
                })
                const setMessageTop = await getDataByKey(chatDb, "message", message.data.messageId);
                setMessageTop.msgTop = 2;
                updateDB(chatDb, "message", setMessageTop);
                break;
            case 10015:
                const setChatUserDelete = await getDataByKey(chatDb, "chatUser", message.data.sendUserId);
                setChatUserDelete.status = 2
                updateDB(chatDb, "chatUser", setChatUserDelete);
                cursorDelete(chatDb, "message", "chatId", message.data.chatId);
                break;
            case 10019:
                const setChatUserClear = await getDataByKey(chatDb, "chatUser", message.data.sendUserId);
                setChatUserClear.clean = 2
                setChatUserClear.lastSendName = ""
                setChatUserClear.lastMessage = "已清空聊天记录"
                setChatUserClear.lastReceiveTime = new Date().getTime()
                updateDB(chatDb, "chatUser", setChatUserClear);
                cursorDelete(chatDb, "message", "chatId", message.data.chatId);
                break;
            case 10004:
            case 10014:
                const updateChatUser = await getDataByKey(chatDb, "chatUser", message.data.contactId);
                updateChatUser.contactName = message.data.name
                updateChatUser.contactIcon = message.data.icon
                updateDB(chatDb, "chatUser", updateChatUser)
                break;
        }
        messageCallBack && messageCallBack(message)
    };
    ws.onclose = function () {
        reconnect('onclose')
    };
    ws.onerror = function () {
        reconnect('onerror')
    };
    const reconnect = (type) => {
        if (!needReconnect) {
            return;
        }
        if (ws != null) {
            ws.close()
        }
        if (lockReconnect) {
            return;
        }
        lockReconnect = true;
        if (maxReConnectTimes > 0) {
            console.log('准备重连ws，剩余重连ws次数' + maxReConnectTimes, new Date().getTime())
            maxReConnectTimes--
            setTimeout(function () {
                createWs()
                lockReconnect = false;
            }, 5000)
        } else {
            console.log('连接ws已超时')
        }
    };
    setInterval(() => {
        if (ws != null && ws.readyState == 1) {
            ws.send('heart beat')
        }
    }, 1000 * 5);
}

const onMessage = (callback) => {
    messageCallBack = callback;
}

const closeWs = () => {
    needReconnect = false;
    ws.close();
}

export {
    initWs,
    onMessage,
    closeWs
}
